cap log close
log using "${logpath}firm_eventstudy.log", replace

/*******************************************************************************
firm_eventstudy.do

This code creates event studies for incapacitated firms
*******************************************************************************/

qui {
	use "${cleandatapath}ambulancetaxis_firmpanel_enforcement.dta", clear

	** Make time variables 
	gen firmenforcementmonth = mofd(firmenforcementdate)
	drop if mi(firmenforcementmonth)

	* Create relative time variable
	gen reltime = month - firmenforcementmonth

	** Remove firms treated too close to start/end of panel
	drop if firmenforcementmonth < mofd(mdy(1,1,2009)) & !mi(firmenforcementmonth)
	drop if firmenforcementmonth >= mofd(mdy(1,1,2018)) & !mi(firmenforcementmonth)

	* Create extensive margin
	gen firmactive = (linepmt > 0)

	** Window
	drop if abs(reltime) > 24
}

collapse (mean) linepmt (mean) ride (mean) firmactive, by(reltime firmenforcementtype_civil)

sort firmenforcementtype_civil reltime

*Figure 12
twoway line linepmt reltime if firmenforcementtype_civil == 1, xline(0, lcolor(gs8)) graphregion(color(white)) xti("Months Until Civil Enforcement") yti("Mean Firm Non-Emergency Dialysis Payments") yscale(range(0 20000)) ylab(#4)
qui graph export "${outpath}Figure_12a.pdf", replace

twoway line linepmt reltime if firmenforcementtype_civil == 0, xline(0, lcolor(gs8)) graphregion(color(white)) xti("Months Until Criminal Enforcement") yti("Mean Firm Non-Emergency Dialysis Payments") yscale(range(0 20000))
qui graph export "${outpath}Figure_12b.pdf", replace

*Figure A19
twoway line firmactive reltime if firmenforcementtype_civil == 0, xline(0, lcolor(gs8)) graphregion(color(white)) xti("Months Until Criminal Enforcement") yti("Share Firms Active") yscale(range(0 .8)) ylab(#5)
qui graph export "${outpath}Figure_A19d.pdf", replace

twoway line firmactive reltime if firmenforcementtype_civil == 1, xline(0, lcolor(gs8)) graphregion(color(white)) xti("Months Until Civil Enforcement") yti("Share Firms Active") yscale(range(0 .8)) ylab(#5)
qui graph export "${outpath}Figure_A19c.pdf", replace

twoway line ride reltime if firmenforcementtype_civil == 0, xline(0, lcolor(gs8)) graphregion(color(white)) xti("Months Until Criminal Enforcement") yti("Mean Firm Non-Emergency Dialysis Rides") xlab(-24 -12 0 12 24) yscale(range(0 80)) ylab(#5)
qui graph export "${outpath}Figure_A19b.pdf", replace

twoway line ride reltime if firmenforcementtype_civil == 1, xline(0, lcolor(gs8)) graphregion(color(white)) xti("Months Until Civil Enforcement") yti("Mean Firm Non-Emergency Dialysis Rides") xlab(-24 -12 0 12 24) yscale(range(0 80))  ylab(#5)
qui graph export "${outpath}Figure_A19a.pdf", replace

log close